<?php
namespace App\Repository\User;

use App\Facade\Event;
use App\Event\User\MoneyEvent;
use App\Event\User\MoneyAllEvent;
use App\Event\User\MoneyUserEvent;
use App\Event\User\MoneyIntegralEvent;

Trait Money
{
    // 使用
    public function moneyMake($money, $content)
    {
        if($this->moneyHas($money))
        {
            return $this->moneyAdd(-1 * $money, $content);
        }
    }
    
    // 判断
    public function moneyHas($money)
    {
        if($money > 0)
        {
            return $this->model->money >= $money;
        }
    }
    
    // 增加
    public function moneyAdd($money, $content)
    {
        $res = $this->model->increment('money', $money);
        
        Event::dispatch(new MoneyEvent($this, $money, $content));
            
        return $res;
    }
    
    // 累计
    public function moneyAll($money)
    {
        $this->model->increment('money_all', $money);
        
        Event::dispatch(new MoneyAllEvent($this, $money));
    }
    
    // 团队
    public function moneyUser($money)
    {
        $this->model->increment('money_user', $money);
        
        Event::dispatch(new MoneyUserEvent($this, $money));
    }
    
    // 判断业绩
    public function moneyAllHas($money)
    {
        return $this->model->money_all >= $money;
    }
    
    // 更新远程商城积分
    public function moneyIntegral($money)
    {
        Event::dispatch(new MoneyIntegralEvent($this, $money));
    }
}